package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.redis.SecKillKeyPrefix;
import cn.wolfcode.shop.cloud.util.SeckillCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController("/getPath")
public class PathController {
    @Autowired
    private RedisService redisService;
    @RequestMapping
    public Result<String> getPath(String uuid,String verifyCode){
        if (StringUtils.isEmpty(verifyCode)){
            throw new BusinessException(SeckillCodeMsg.VERIEY_CODE_NOT_EMPTY);
        }
        String result = redisService.get(SecKillKeyPrefix.SECKILL_VERIFY_CODE, uuid, String.class);
       //redis缓存里面没有就报验证码错误
        if (!verifyCode.equals(result)){
            throw new BusinessException(SeckillCodeMsg.VERIEY_CODE_ERROP);
        }

        //生成uuid
        String path = UUID.randomUUID().toString().replace("-", "");
        //放进redis
        redisService.set(SecKillKeyPrefix.SECKILL_PATH,uuid,path);
        return Result.success(path);
    }

}
